<template>
  <div class="jz idCard">
    <a-form-model :rules="loginFormRules" :model="loginForm" ref="loginFormRef">
      <a-form-model-item prop="username" class="username">
        <a-input v-model="loginForm.username" placeholder="用户名" size="large">
          <a-icon class="icon" slot="addonBefore" type="user" />
        </a-input>
      </a-form-model-item>
      <a-form-model-item class="passwd" prop="passwd">
        <a-input v-model="loginForm.passwd" placeholder="密码" size="large" type="password">
          <a-icon class="icon" slot="addonBefore" type="lock" />
        </a-input>
      </a-form-model-item>
    </a-form-model>
    <a-checkbox class="checkBox" v-model="checked">记住用户名和密码</a-checkbox>
    <a-button class="loginButton" @click="login" type="primary" size="large" :loading="loginLoading" block>登录</a-button>
  </div>
</template>

<script>
import { postUserInformation } from "../../api/api/post.js";
export default {
  data() {
    return {
      loginForm: {
        username: "",
        passwd: ""
      },
      data: [],
      loginLoading: false,
      checked: "",
      loginFormRules: {
        username: [
          { required: true, message: "用户名不能为空", tigger: "blur" }
        ],
        passwd: [{ required: true, message: "密码不能为空", tigger: "blur" }]
      }
    };
  },

  //得到cookie
  mounted() {
    this.getCookie();
    if (this.loginForm.username != "" && this.loginForm.passwd != "") {
      this.checked = true;
      // console.log("this.loginForm", this.loginForm);
    } else {
      this.checked = false;
    }
  },
  methods: {
    login() {
      this.loginLoading = true;
      //判断复选框是否被勾选 勾选则调用配置cookie方法
      if (this.checked == true) {
        //传入账号名，密码，和保存天数3个参数
        this.setCookie(this.loginForm.username, this.loginForm.passwd, 7);
      } else {
        //清空Cookie
        this.clearCookie();
      }

      //与后端请求代码
      const params = this.loginForm;
      if (params) {
        this.$refs.loginFormRef.validate(valid => {
          if (!valid) return;

          postUserInformation(params).then(res => {
            if (res.id) {
              sessionStorage.setItem("this.data", JSON.stringify(res));
              this.$router.push("/statistics");
              this.loginLoading = false;
              return this.$notification.open({
                message: "登录成功"
              });
            }
            this.$notification.open({
              message: "登录失败,请重新登录"
            });
            this.loginLoading = false;
          });
        });
      }
    },

    // 设置cookie
    setCookie(c_name, c_pwd, exdays) {
      var exdate = new Date(); // 获取时间
      exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); // 保存的天数
      // 字符串拼接cookie
      window.document.cookie =
        "userName" + "=" + c_name + ";path=/Login;expires=" + exdate.toGMTString();
      window.document.cookie =
        "userPwd" + "=" + c_pwd + ";path=/Login;expires=" + exdate.toGMTString();
    },

    //读取cookie
    getCookie: function() {
      if (document.cookie.length > 0) {
        var arr = document.cookie.split("; ");
        for (var i = 0; i < arr.length; i++) {
          var arr2 = arr[i].split("="); // 再次切割
          // 判断查找相对应的值
          if (arr2[0] == "userName") {
            this.loginForm.username = arr2[1]; // 保存到保存数据的地方
          } else if (arr2[0] == "userPwd") {
            this.loginForm.passwd = arr2[1];
          }
        }
      }
    },
    clearCookie: function() {
      this.setCookie("", "", -1); //修改2值都为空，天数为负1天就好了
    }
  }
};
</script>

<style scoped>
.idCard {
  position: relative;
  padding: 44px 35px 44px 36px;
  width: 394px;
  height: 327px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.1);
}
.username {
  position: relative;
  margin-bottom: 27px;
}
.passwd {
  position: absolute;
  top: 110px;
  left: 36px;
  width: 323px;
}
.icon {
  font-size: 18px;
}
.checkBox {
  position: absolute;
  display: block;
  top: 182px;
  left: 46px;
}
.loginButton {
  position: absolute;
  top: 248px;
  left: 36px;
  width: 323px;
}
</style>